home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / B-C / C++Source Code Fmtr Folder / Src / DFile.cp < prev    next >
Encoding:
Text File  |  1992-04-27  |  1.7 KB  |  91 lines  |  [TEXT/MPS ]

  1. #ifndef __CDENT__
  2. #include "cdent.h"
  3. #endif
  4.  
  5. #ifndef __DFILE__
  6. #include "DFile.h"
  7. #endif
  8.  
  9. #ifndef __STRING__
  10. #include <string.h>
  11. #endif
  12.  
  13.  
  14.  
  15. //µ   DFile::IDFile
  16. #pragma segment DFile
  17. short DFile::IDFile(const DFile *anArea)
  18. {
  19.     fOutput = anArea->fOutput;
  20.     return (IDataArea(anArea));
  21. }
  22.  
  23.  
  24. //µ   DFile::SetOutput
  25. #pragma segment DFile
  26. void DFile::SetOutput(StdFile *anOutput)
  27. {
  28.     // If changing files, write the data that's accumulated so far into the
  29.     // given output file.
  30.     if (fOutput != anOutput && fOutput != 0)
  31.         Flush();
  32.     fOutput = anOutput;
  33. }
  34.  
  35.  
  36. //µ   DFile::Putc
  37. #pragma segment DFile
  38. void DFile::Putc(int aChar)
  39. {
  40.     char c = (char)aChar;
  41.  
  42.     if (Write(&c, sizeof(char)) != sizeof(char))
  43.         diag(kFatal, "Out of memory in DFile::Putc\n");
  44. }
  45.  
  46.  
  47. //µ   DFile::Puts
  48. #pragma segment DFile
  49. void DFile::Puts(const char *aString)
  50. {
  51.     int aLen = strlen(aString);
  52.  
  53.     if (Write(aString, aLen) != aLen)
  54.         diag(kFatal, "Out of memory in DFile::Puts\n");
  55.     Putc('\n');
  56. }
  57.  
  58.  
  59. //µ   DFile::Flush
  60. #pragma segment DFile
  61. size_t DFile::Flush(size_t aThreshold)
  62. {
  63.     if (fOutput == 0)
  64.         return (0);
  65.  
  66.     // If there is less data than the threshold, return 0.  ClearErr() on
  67.     // fOutput so that after calling Error() and getting the information that
  68.     // no error occurred, the caller knows that no data was written because
  69.     // the threshold was not reached.
  70.     if (aThreshold > GetCursor()) {
  71.         fOutput->ClearErr();
  72.         return (0);
  73.     }
  74.  
  75.     // Move the data to a safe place and make sure it doesn't move.
  76.     MoveHHi();
  77.     HLock();
  78.  
  79.     // Write the data.
  80.     size_t amtWritten = fOutput->Write(GetData(0), GetCursor());
  81.  
  82.     // All done, unlock the data, clear the buffer for more data
  83.     HUnlock();
  84.     SetCursor(0);
  85.  
  86.     // Return the amount written
  87.     return (amtWritten);
  88. }
  89.  
  90.  
  91.